package com.mxpio.erp.mes.controller;

import com.mxpio.erp.common.mes.entity.ManufactureWorkOrder;
import com.mxpio.erp.common.mes.pojo.ManufactureWorkOrderVo;
import com.mxpio.erp.common.mes.service.ManufactureWorkOrderService;
import com.mxpio.erp.common.workshop.entity.ManufactureOrder;
import com.mxpio.erp.common.workshop.service.ManufactureOrderService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ManufactureWorkOrderController", description = "生产订单-工单接口")
@RestController
@RequestMapping("/erp/mes/mowo/")
public class ManufactureWorkOrderController {
	@Autowired
	private ManufactureWorkOrderService service;

	@Autowired
	private ManufactureOrderService manufactureOrderService;

	@GetMapping("list")
	@Operation(summary = "生产订单-工单列表", description = "获取生产订单-工单列表", method = "GET")
	public Result<List<ManufactureWorkOrder>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<ManufactureWorkOrder> items = service.list(ManufactureWorkOrder.class, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("page")
	@Operation(summary = "生产订单-工单列表", description = "获取生产订单-工单列表", method = "GET")
	public Result<Page<ManufactureWorkOrder>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<ManufactureWorkOrder> items = service.listPage(ManufactureWorkOrder.class, page, criteria);
		return Result.OK(items);
	}
	
	@GetMapping("list/{batchCode}")
	@Operation(summary = "根据batchCode获取生产订单-工单", description = "根据batchCode获取生产订单-工单", method = "GET")
	public Result<ManufactureWorkOrder> getById(@PathVariable(name = "batchCode", required = true) String batchCode) {
		ManufactureWorkOrder item = service.getById(ManufactureWorkOrder.class, batchCode);
		return Result.OK(item);
	}

	@DeleteMapping("remove/{batchCodes}")
	@Operation(summary = "根据batchCode删除", description = "删除", method = "DELETE")
	public Result<ManufactureWorkOrder> remove(@Parameter(description="产碰编码，多个单号用英文逗号分隔") @PathVariable(name = "batchCodes", required = true) String batchCodes) {
		String[] codeArray = batchCodes.split(",");
		for(String key : codeArray){
			service.delete(ManufactureWorkOrder.class, key);
		}
		return Result.OK();
	}
	@PutMapping("mo_dispatch")
	@Operation(summary = "生产订单 下发工单", description = "生产订单 下发工单", method = "PUT")
	public Result<?> manufactureOrderDispatch(@RequestBody ManufactureWorkOrderVo batchInfoVo) {
		return service.manufactureOrderDispatch(batchInfoVo);
	}

	@GetMapping("pre_invest_num/{bizNo}")
	@Operation(summary = "根据bizNo获取生产订单-已下发数量", description = "根据bizNo获取生产订单-已下发数量", method = "GET")
	public Result<Integer> getMoPreInvestNumByBizNo(@PathVariable(name = "bizNo", required = true) String bizNo) {
		Criteria criteria = Criteria.create();
		criteria.addCriterion("bizNo", Operator.EQ,bizNo);
		List<ManufactureWorkOrder> list = service.list(ManufactureWorkOrder.class, criteria);
		if (list.isEmpty()){
			return Result.OK(0);
		}
		int preInvestNum = list.stream().mapToInt(ManufactureWorkOrder::getBatchNum).reduce(0, Integer::sum);
		return Result.OK(preInvestNum);
	}
	@GetMapping("mopage")
	@Operation(summary = "生产订单列表", description = "获取生产订单列表（分页）", method = "GET")
	public Result<Page<ManufactureOrder>> mopage(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<ManufactureOrder> pageResult = manufactureOrderService.listPage(ManufactureOrder.class, page, criteria);
		if (!pageResult.getContent().isEmpty()){
			service.handleDispatchNum(pageResult.getContent());
		}
		return Result.OK(pageResult);
	}

	@RequestMapping("release/{batchCode}")
	@Operation(summary = "生产订单-工单-下达", description = "生产订单-工单-下达")
	public Result<?> release(@PathVariable(name = "batchCode", required = true) String batchCode) {
		return service.release(batchCode);
	}

}
